package org.gdstash.ui;

import org.gdstash.db.DBAffix;
import org.gdstash.db.DBAffixSet;
import org.gdstash.db.DBLootTable;
import org.gdstash.db.DBLootTableSet;
import org.gdstash.item.GDItem;
import org.gdstash.ui.select.HexLenDocFilter;
import org.gdstash.ui.select.IntLenDocFilter;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.ui.util.WideComboBox;
import org.gdstash.util.GDConstants;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class GDItemCraftPane extends AdjustablePanel {
    private JLabel lblImage;
    private JLabel lblName;
    private JLabel lblPrefix;
    private JLabel lblSuffix;
    private JLabel lblModifier;
    private JLabel lblCompletion;
    private JLabel lblSeed;
    private JLabel lblCount;
    private JLabel txtName;
    private JComboBox<DBAffix> cbPrefix;
    private JComboBox<DBAffix> cbSuffix;
    private JComboBox<DBAffix> cbModifier;
    private JComboBox<DBAffix> cbCompletion;
    private JFormattedTextField ftSeed;
    private JFormattedTextField ftCount;
    private List<DBLootTable> itemLootTables;
    private List<DBAffix> allPrefixes;
    private List<DBAffix> allSuffixes;
    private List<DBAffix> filterPrefixes;
    private List<DBAffix> filterSuffixes;
    private boolean skipCombo;
    private DefaultComboBoxModel<DBAffix> dmPrefix;
    private DefaultComboBoxModel<DBAffix> dmSuffix;
    private DefaultComboBoxModel<DBAffix> dmModifier;
    private DefaultComboBoxModel<DBAffix> dmModifierCelestial;
    private DefaultComboBoxModel<DBAffix> dmCompletion;
    private JLabel txtDescription;
    private GDItem item;

    public GDItemCraftPane() {
        this.skipCombo = false;
        this.allPrefixes = new LinkedList<DBAffix>();
        this.allSuffixes = new LinkedList<DBAffix>();
        this.filterPrefixes = new LinkedList<DBAffix>();
        this.filterSuffixes = new LinkedList<DBAffix>();
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel panel = this.buildInfoPanel();
        (this.lblImage = new JLabel()).setBorder(compound);
        this.lblImage.setHorizontalAlignment(0);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage).addComponent(panel).addComponent(this.txtDescription));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage)).addGroup(layout.createParallelGroup().addComponent(panel)).addGroup(layout.createParallelGroup().addComponent(this.txtDescription));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, panel, this.lblImage);
        layout.linkSize(0, panel, this.txtDescription);
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        final Dimension dimCBMax = new Dimension(50 * size, 4 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntFText = UIManager.getDefaults().getFont("FormattedTextField.font");
        if (fntFText == null) {
            fntFText = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) size);
        fntCombo = fntCombo.deriveFont((float) size);
        fntFText = fntFText.deriveFont((float) size);
        Border compound = null;
        if (this.txtName == null || this.txtDescription == null) {
            final Border lowered = BorderFactory.createEtchedBorder(1);
            final Border raised = BorderFactory.createEtchedBorder(0);
            compound = BorderFactory.createCompoundBorder(raised, lowered);
        }
        if (this.lblName == null) {
            this.lblName = new JLabel();
        }
        this.lblName.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_NAME"));
        this.lblName.setFont(fntLabel);
        if (this.txtName == null) {
            (this.txtName = new JLabel()).setBorder(compound);
        }
        this.txtName.setFont(fntLabel);
        this.txtName.setPreferredSize(dimMax);
        this.txtName.setMaximumSize(dimMax);
        if (this.lblPrefix == null) {
            this.lblPrefix = new JLabel();
        }
        this.lblPrefix.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_PREFIX"));
        this.lblPrefix.setFont(fntLabel);
        if (this.cbPrefix == null) {
            (this.cbPrefix = new WideComboBox<DBAffix>()).setMaximumRowCount(25);
            this.cbPrefix.addActionListener(new ComboBoxActionListener());
        }
        this.cbPrefix.setFont(fntCombo);
        this.cbPrefix.setPreferredSize(dimCBMax);
        this.cbPrefix.setMaximumSize(dimCBMax);
        if (this.lblSuffix == null) {
            this.lblSuffix = new JLabel();
        }
        this.lblSuffix.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SUFFIX"));
        this.lblSuffix.setFont(fntLabel);
        if (this.cbSuffix == null) {
            (this.cbSuffix = new WideComboBox<DBAffix>()).setMaximumRowCount(25);
            this.cbSuffix.addActionListener(new ComboBoxActionListener());
        }
        this.cbSuffix.setFont(fntCombo);
        this.cbSuffix.setPreferredSize(dimCBMax);
        this.cbSuffix.setMaximumSize(dimCBMax);
        if (this.lblModifier == null) {
            this.lblModifier = new JLabel();
        }
        this.lblModifier.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_MODIFIER"));
        this.lblModifier.setFont(fntLabel);
        if (this.cbModifier == null) {
            (this.cbModifier = new WideComboBox<DBAffix>()).addActionListener(new ComboBoxActionListener());
        }
        this.cbModifier.setFont(fntCombo);
        if (this.lblCompletion == null) {
            this.lblCompletion = new JLabel();
        }
        this.lblCompletion.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_COMPLETION"));
        this.lblCompletion.setFont(fntLabel);
        if (this.cbCompletion == null) {
            (this.cbCompletion = new WideComboBox<DBAffix>()).addActionListener(new ComboBoxActionListener());
        }
        this.cbCompletion.setFont(fntCombo);
        if (this.lblSeed == null) {
            this.lblSeed = new JLabel();
        }
        this.lblSeed.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SEED"));
        this.lblSeed.setFont(fntLabel);
        if (this.ftSeed == null) {
            this.ftSeed = new JFormattedTextField();
            final DocumentFilter filter = new HexLenDocFilter(8);
            final AbstractDocument doc = (AbstractDocument) this.ftSeed.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftSeed.setFont(fntFText);
        if (this.lblCount == null) {
            this.lblCount = new JLabel();
        }
        this.lblCount.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_COUNT"));
        this.lblCount.setFont(fntLabel);
        if (this.ftCount == null) {
            this.ftCount = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(4);
            final AbstractDocument doc = (AbstractDocument) this.ftCount.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftCount.setFont(fntFText);
        if (this.txtDescription == null) {
            (this.txtDescription = new JLabel()).setBorder(compound);
        }
        this.txtDescription.setFont(fntLabel);
    }

    public void refresh() {
        this.setItem(null, 0);
    }

    public static List<DBLootTableSet> extractTableSets(final List<DBLootTable> tables) {
        final List<DBLootTableSet> setAll = new LinkedList<DBLootTableSet>();
        if (tables == null) {
            return setAll;
        }
        for (final DBLootTable table : tables) {
            if (table.getAffixSetAllocList() == null) {
                continue;
            }
            final List<DBLootTableSet> sets = DBLootTableSet.getByTableID(table.getTableID());
            if (sets == null) {
                continue;
            }
            for (final DBLootTableSet set : sets) {
                if (set == null) {
                    continue;
                }
                boolean found = false;
                for (final DBLootTableSet ts : setAll) {
                    if (ts.getTableSetID().equals(set.getTableSetID())) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    continue;
                }
                setAll.add(set);
            }
        }
        return setAll;
    }

    private void setInfo(final GDItem.LabelInfo info, final JLabel label) {
        if (info == null) {
            label.setText("");
        } else {
            label.setForeground(info.foreground);
            label.setText(info.text);
        }
    }

    public void setNameInfo(final GDItem.LabelInfo info) {
        this.setInfo(info, this.txtName);
    }

    public void setImage(final Icon icon) {
        this.lblImage.setIcon(icon);
    }

    public void setItem(final GDItem item, final int location) {
        if (item == null) {
            this.item = null;
        } else {
            this.item = item.clone();
        }
        this.initItemInfo(location);
    }

    private void initItemInfo(final int location) {
        if (this.item == null) {
            final GDItem.LabelInfo li = new GDItem.LabelInfo();
            li.text = "";
            this.setImage(null);
            this.setNameInfo(li);
            this.itemLootTables = null;
            this.cbPrefix.removeAllItems();
            this.cbPrefix.setEnabled(false);
            this.cbSuffix.removeAllItems();
            this.cbSuffix.setEnabled(false);
            this.cbModifier.removeAllItems();
            this.cbModifier.setEnabled(false);
            this.cbCompletion.removeAllItems();
            this.cbCompletion.setEnabled(false);
            this.ftSeed.setText(null);
            this.ftCount.setText(null);
        } else {
            this.setImage(this.item.getImageIcon());
            this.setNameInfo(this.item.getItemNameInfo());
            this.fillCombos(this.item);
            GDMsgLogger.showLog(this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
            this.cbPrefix.setModel(this.dmPrefix);
            this.cbSuffix.setModel(this.dmSuffix);
            if (this.item.isCraftable() || GDStashFrame.iniConfig.sectRestrict.blacksmithAll) {
                this.cbModifier.setModel(this.dmModifier);
            } else if (this.item.getRarity().equals("Legendary")) {
                this.cbModifier.setModel(this.dmModifierCelestial);
            } else {
                this.cbModifier.removeAllItems();
            }
            this.cbCompletion.setModel(this.dmCompletion);
            if (location == 1) {
                this.cbPrefix.setEnabled(false);
                this.cbSuffix.setEnabled(false);
                this.cbModifier.setEnabled(false);
                this.cbCompletion.setEnabled(false);
                this.ftSeed.setText(this.item.getSeedHex());
                this.ftCount.setText(this.item.getStackCountStr());
                this.ftSeed.setEditable(false);
                this.ftCount.setEditable(false);
            } else {
                this.cbPrefix.setEnabled(this.dmPrefix.getSize() > 1);
                this.cbSuffix.setEnabled(this.dmSuffix.getSize() > 1);
                if (this.item.isCraftable() || GDStashFrame.iniConfig.sectRestrict.blacksmithAll) {
                    this.cbModifier.setEnabled(this.dmModifier.getSize() > 1);
                } else if (this.item.getRarity().equals("Legendary")) {
                    this.cbModifier.setEnabled(this.dmModifierCelestial.getSize() > 1);
                } else {
                    this.cbModifier.setEnabled(false);
                }
                this.cbCompletion.setEnabled(this.dmCompletion.getSize() > 1);
                this.ftSeed.setText(null);
                this.ftCount.setText(null);
                this.ftSeed.setEditable(true);
                this.ftCount.setEditable(true);
            }
        }
        this.updateDescription();
    }

    private void updateDescription() {
        String s = null;
        if (this.item == null) {
            s = "";
        } else {
            s = this.item.getFullDescription();
        }
        this.txtDescription.setText(s);
    }

    public static List<DBAffixSet> extractAffixSets(final List<DBLootTable> tables) {
        final List<DBAffixSet> setAll = new LinkedList<DBAffixSet>();
        if (tables == null) {
            return setAll;
        }
        for (final DBLootTable table : tables) {
            if (table.getAffixSetAllocList() == null) {
                continue;
            }
            final List<DBAffixSet> sets = DBAffixSet.getByAffixSetIDs(table.getAffixSetIDs());
            if (sets == null) {
                continue;
            }
            for (final DBAffixSet set : sets) {
                if (set == null) {
                    continue;
                }
                boolean found = false;
                for (final DBAffixSet as : setAll) {
                    if (as.getAffixSetID().equals(set.getAffixSetID())) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    continue;
                }
                setAll.add(set);
            }
        }
        return setAll;
    }

    private void fillPrefixCombo() {
        List<DBAffix> currPrefixes = null;
        if (GDStashFrame.iniConfig != null && (GDStashFrame.iniConfig.sectRestrict.affixCombi == 3 || GDStashFrame.iniConfig.sectRestrict.affixCombi == 2)) {
            currPrefixes = this.filterPrefixes;
        } else {
            currPrefixes = new LinkedList<DBAffix>();
            for (final DBLootTable table : this.itemLootTables) {
                DBLootTable.mixAffixes(this.item, currPrefixes, table.getFilterPrefixesForSuffix(this.item.getSuffix(), this.filterPrefixes));
            }
        }
        Collections.<DBAffix>sort(currPrefixes, new DBAffix.AffixComparator());
        this.skipCombo = true;
        this.dmPrefix.setSelectedItem(null);
        this.dmPrefix.removeAllElements();
        this.dmPrefix.addElement(null);
        boolean found = false;
        final DBAffix prefix = this.item.getPrefix();
        for (final DBAffix affix : currPrefixes) {
            this.dmPrefix.addElement(affix);
            if (prefix != null && prefix.getAffixID().equals(affix.getAffixID())) {
                found = true;
            }
        }
        if (found) {
            this.dmPrefix.setSelectedItem(this.item.getPrefix());
            this.skipCombo = false;
        } else {
            this.skipCombo = false;
            this.item.setPrefix(null);
        }
    }

    private void fillSuffixCombo() {
        List<DBAffix> currSuffixes = null;
        if (GDStashFrame.iniConfig != null && (GDStashFrame.iniConfig.sectRestrict.affixCombi == 3 || GDStashFrame.iniConfig.sectRestrict.affixCombi == 2)) {
            currSuffixes = this.filterSuffixes;
        } else {
            currSuffixes = new LinkedList<DBAffix>();
            for (final DBLootTable table : this.itemLootTables) {
                DBLootTable.mixAffixes(this.item, currSuffixes, table.getFilterSuffixesForPrefix(this.item.getPrefix(), this.filterSuffixes));
            }
        }
        Collections.<DBAffix>sort(currSuffixes, new DBAffix.AffixComparator());
        this.skipCombo = true;
        this.dmSuffix.setSelectedItem(null);
        this.dmSuffix.removeAllElements();
        this.dmSuffix.addElement(null);
        boolean found = false;
        final DBAffix suffix = this.item.getSuffix();
        for (final DBAffix affix : currSuffixes) {
            this.dmSuffix.addElement(affix);
            if (suffix != null && suffix.getAffixID().equals(affix.getAffixID())) {
                found = true;
            }
        }
        if (found) {
            this.dmSuffix.setSelectedItem(this.item.getSuffix());
            this.skipCombo = false;
        } else {
            this.skipCombo = false;
            this.item.setSuffix(null);
        }
    }

    public static List<DBAffix> extractAffixes(final GDItem item, final List<DBAffixSet> sets, final int affixType) {
        List<DBAffix> affixAll = new LinkedList<DBAffix>();
        if (GDStashFrame.iniConfig != null && GDStashFrame.iniConfig.sectRestrict.affixCombi == 3) {
            affixAll = DBAffix.getFullAffixList();
        }
        if (sets == null) {
            return affixAll;
        }
        for (final DBAffixSet set : sets) {
            if (set == null) {
                continue;
            }
            if (set.getAffixEntries() == null) {
                continue;
            }
            for (final DBAffixSet.DBEntry entry : set.getAffixEntries()) {
                if (entry == null) {
                    continue;
                }
                boolean found = false;
                if (affixType == 4) {
                    for (final DBAffix affix : affixAll) {
                        if (affix.getAffixID().equals(entry.getAffixID())) {
                            found = true;
                            affix.setAffixType(4);
                            break;
                        }
                    }
                }
                if (!found) {
                    for (final DBAffix affix : affixAll) {
                        if (affix.getAffixID().equals(entry.getAffixID())) {
                            found = true;
                            if (affixType != -1) {
                                affix.setAffixType(affixType);
                                break;
                            }
                            break;
                        }
                    }
                }
                if (found) {
                    continue;
                }
                final DBAffix affix2 = DBAffix.get(entry.getAffixID());
                if (affix2 == null) {
                    continue;
                }
                if (affixType != -1) {
                    affix2.setAffixType(affixType);
                }
                affixAll.add(affix2);
            }
        }
        return affixAll;
    }

    private JPanel buildInfoPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblName).addComponent(this.lblPrefix).addComponent(this.lblSuffix).addComponent(this.lblModifier).addComponent(this.lblCompletion).addComponent(this.lblSeed).addComponent(this.lblCount)).addGroup(layout.createParallelGroup().addComponent(this.txtName).addComponent(this.cbPrefix).addComponent(this.cbSuffix).addComponent(this.cbModifier).addComponent(this.cbCompletion).addComponent(this.ftSeed).addComponent(this.ftCount));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblName).addComponent(this.txtName)).addGroup(layout.createParallelGroup().addComponent(this.lblPrefix).addComponent(this.cbPrefix)).addGroup(layout.createParallelGroup().addComponent(this.lblSuffix).addComponent(this.cbSuffix)).addGroup(layout.createParallelGroup().addComponent(this.lblModifier).addComponent(this.cbModifier)).addGroup(layout.createParallelGroup().addComponent(this.lblCompletion).addComponent(this.cbCompletion)).addGroup(layout.createParallelGroup().addComponent(this.lblSeed).addComponent(this.ftSeed)).addGroup(layout.createParallelGroup().addComponent(this.lblCount).addComponent(this.ftCount));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblName, this.lblPrefix);
        layout.linkSize(0, this.lblName, this.lblSuffix);
        layout.linkSize(0, this.lblName, this.lblModifier);
        layout.linkSize(0, this.lblName, this.lblCompletion);
        layout.linkSize(0, this.lblName, this.lblSeed);
        layout.linkSize(0, this.lblName, this.lblCount);
        layout.linkSize(0, this.txtName, this.cbPrefix);
        layout.linkSize(0, this.txtName, this.cbSuffix);
        layout.linkSize(0, this.txtName, this.cbModifier);
        layout.linkSize(0, this.txtName, this.cbCompletion);
        layout.linkSize(0, this.txtName, this.ftSeed);
        layout.linkSize(0, this.txtName, this.ftCount);
        layout.linkSize(1, this.lblName, this.txtName);
        layout.linkSize(1, this.lblPrefix, this.cbPrefix);
        layout.linkSize(1, this.lblSuffix, this.cbSuffix);
        layout.linkSize(1, this.lblModifier, this.cbModifier);
        layout.linkSize(1, this.lblCompletion, this.cbCompletion);
        layout.linkSize(1, this.lblSeed, this.ftSeed);
        layout.linkSize(1, this.lblCount, this.ftCount);
        return panel;
    }

    private void fillCombos(final GDItem item) {
        this.dmPrefix = new DefaultComboBoxModel<DBAffix>();
        this.dmSuffix = new DefaultComboBoxModel<DBAffix>();
        this.dmModifier = new DefaultComboBoxModel<DBAffix>();
        this.dmModifierCelestial = new DefaultComboBoxModel<DBAffix>();
        this.dmCompletion = new DefaultComboBoxModel<DBAffix>();
        this.dmPrefix.addElement(null);
        this.dmSuffix.addElement(null);
        this.dmModifier.addElement(null);
        this.dmModifierCelestial.addElement(null);
        this.dmCompletion.addElement(null);
        this.dmPrefix.setSelectedItem(null);
        this.dmSuffix.setSelectedItem(null);
        this.dmModifier.setSelectedItem(null);
        this.dmModifierCelestial.setSelectedItem(null);
        this.dmCompletion.setSelectedItem(null);
        if (item.getItemID() == null) {
            return;
        }
        if (item.isArmor() || item.isJewelry() || item.isWeapon() || item.isArtifact()) {
            this.dmModifier = GDStashFrame.getModifier();
            this.dmModifierCelestial = GDStashFrame.getCelestialModifier();
            this.dmModifier.setSelectedItem(null);
            this.dmModifierCelestial.setSelectedItem(null);
        }
        List<DBAffix> affixAll = null;
        final List<DBAffixSet> affixSets = new LinkedList<DBAffixSet>();
        if (item.isArtifact() && item.getBonusAffixSet() != null) {
            affixSets.add(item.getBonusAffixSet());
        }
        affixAll = extractAffixes(item, affixSets, -1);
        if (GDStashFrame.iniConfig == null || GDStashFrame.iniConfig.sectRestrict.affixCombi != 3) {
            this.itemLootTables = DBLootTable.getByItemID(item.getItemID());
            for (final DBLootTable table : this.itemLootTables) {
                DBLootTable.mixAffixes(item, affixAll, table.getAllAffixes());
            }
        }
        Collections.<DBAffix>sort(affixAll, new DBAffix.AffixComparator());
        this.allPrefixes.clear();
        this.allSuffixes.clear();
        this.filterPrefixes.clear();
        this.filterSuffixes.clear();
        for (final DBAffix affix : affixAll) {
            if (affix == null) {
                continue;
            }
            if (affix.getAffixType() == 1) {
                affix.resetDescription();
                this.allPrefixes.add(affix);
                this.filterPrefixes.add(affix);
            }
            if (affix.getAffixType() == 2) {
                this.allSuffixes.add(affix);
                this.filterSuffixes.add(affix);
            }
            if (affix.getAffixType() != 4) {
                continue;
            }
            if (GDStashFrame.iniConfig.sectRestrict.completionAll) {
                continue;
            }
            if (!item.isArtifact()) {
                continue;
            }
            this.dmCompletion.addElement(affix);
        }
        if (item.isArtifact()) {
            if (GDStashFrame.iniConfig.sectRestrict.completionAll) {
                this.dmCompletion = GDStashFrame.getCompletion();
            }
            if (!item.getBonusAffixList().isEmpty()) {
                for (final DBAffix affix : item.getBonusAffixList()) {
                    boolean found = false;
                    for (int i = 0; i < this.dmCompletion.getSize(); ++i) {
                        final DBAffix dba = this.dmCompletion.getElementAt(i);
                        if (affix.equals(dba)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        this.dmCompletion.addElement(affix);
                    }
                }
            }
        }
        this.fillPrefixCombo();
        this.fillSuffixCombo();
        if (item.getPrefix() != null) {
            this.dmPrefix.setSelectedItem(item.getPrefix());
        }
        if (item.getSuffix() != null) {
            this.dmSuffix.setSelectedItem(item.getSuffix());
        }
        if (item.getModifier() != null) {
            this.dmModifier.setSelectedItem(item.getModifier());
        }
        if (item.getCompletionBonus() != null) {
            this.dmCompletion.setSelectedItem(item.getCompletionBonus());
        }
    }

    public List<DBAffix> getPrefixes() {
        return this.allPrefixes;
    }

    public void setFilteredPrefixes(final List<DBAffix> prefixes) {
        this.filterPrefixes = prefixes;
        this.skipCombo = true;
        final DBAffix prefix = (DBAffix) this.dmPrefix.getSelectedItem();
        boolean found = false;
        this.dmPrefix.setSelectedItem(null);
        this.dmPrefix.removeAllElements();
        this.dmPrefix.addElement(null);
        for (final DBAffix affix : this.filterPrefixes) {
            this.dmPrefix.addElement(affix);
            if (prefix != null && prefix.getAffixID().equals(affix.getAffixID())) {
                found = true;
            }
        }
        if (found) {
            this.dmPrefix.setSelectedItem(prefix);
            this.skipCombo = false;
        } else {
            if (prefix != null) {
                this.dmPrefix.addElement(prefix);
            }
            this.dmPrefix.setSelectedItem(prefix);
            this.skipCombo = false;
        }
    }

    public List<DBAffix> getSuffixes() {
        return this.allSuffixes;
    }

    public void setFilteredSuffixes(final List<DBAffix> suffixes) {
        this.filterSuffixes = suffixes;
        this.skipCombo = true;
        final DBAffix suffix = (DBAffix) this.dmSuffix.getSelectedItem();
        boolean found = false;
        this.dmSuffix.setSelectedItem(null);
        this.dmSuffix.removeAllElements();
        this.dmSuffix.addElement(null);
        for (final DBAffix affix : this.filterSuffixes) {
            this.dmSuffix.addElement(affix);
            if (suffix != null && suffix.getAffixID().equals(affix.getAffixID())) {
                found = true;
            }
        }
        if (found) {
            this.dmSuffix.setSelectedItem(suffix);
            this.skipCombo = false;
        } else {
            if (suffix != null) {
                this.dmSuffix.addElement(suffix);
            }
            this.dmSuffix.setSelectedItem(suffix);
            this.skipCombo = false;
        }
    }

    public GDItem getItem() {
        return this.item;
    }

    public String getSeed() {
        String s = this.ftSeed.getText();
        if (s.length() != 8) {
            s = null;
        } else {
            s = s.toUpperCase(GDConstants.LOCALE_US);
        }
        return s;
    }

    public int getCount() {
        final String s = this.ftCount.getText();
        if (s == null) {
            return 0;
        }
        if (s.isEmpty()) {
            return 0;
        }
        int i = 0;
        try {
            i = Integer.parseInt(s);
        } catch (NumberFormatException ex) {
            i = 0;
        }
        return i;
    }

    private class ComboBoxActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDItemCraftPane.this.item == null) {
                return;
            }
            if (GDItemCraftPane.this.skipCombo) {
                return;
            }
            DBAffix affix = null;
            final JComboBox<DBAffix> cb = (JComboBox<DBAffix>) e.getSource();
            final int i = cb.getSelectedIndex();
            if (i > 0) {
                affix = cb.getItemAt(i);
            }
            if (cb == GDItemCraftPane.this.cbPrefix) {
                GDItemCraftPane.this.item.setPrefix(affix);
                GDItemCraftPane.this.fillPrefixCombo();
            }
            if (cb == GDItemCraftPane.this.cbSuffix) {
                GDItemCraftPane.this.item.setSuffix(affix);
                GDItemCraftPane.this.fillSuffixCombo();
            }
            if (cb == GDItemCraftPane.this.cbModifier) {
                GDItemCraftPane.this.item.setModifier(affix);
            }
            if (cb == GDItemCraftPane.this.cbCompletion) {
                GDItemCraftPane.this.item.setCompletionBonus(affix);
            }
            GDItemCraftPane.this.updateDescription();
            GDMsgLogger.showLog(GDItemCraftPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }
}
